package com.ztmmm.module.exam.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztmmm.common.consts.ExamContant;
import com.ztmmm.common.enums.ApiErrorEnum;
import com.ztmmm.common.vo.ApiRest;
import com.ztmmm.exception.ServiceException;
import com.ztmmm.module.exam.dto.ExamSaveDto;
import com.ztmmm.module.exam.entity.Exam;
import com.ztmmm.module.exam.entity.ExamDepart;
import com.ztmmm.module.exam.entity.ExamUser;
import com.ztmmm.module.exam.mapper.ExamDepartMapper;
import com.ztmmm.module.exam.mapper.ExamMapper;
import com.ztmmm.module.exam.mapper.ExamRepoMapper;
import com.ztmmm.module.exam.mapper.ExamUserMapper;
import com.ztmmm.module.exam.service.IExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ztm
 * @since 2023-03-23
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IExamService {
        @Autowired
        private ExamRepoMapper examRepoMapper;
        @Autowired
        private ExamDepartMapper examDepartMapper;
        @Autowired
        private ExamUserMapper examUserMapper;
    @Transactional
    public ApiRest create(ExamSaveDto examSaveDto){
        checkData(examSaveDto);
        Exam exam = new Exam();
        BeanUtil.copyProperties(examSaveDto,exam,true);
        exam.setId(IdWorker.getIdStr());
        exam.setUpdateTime(new Date());
        exam.setCreateTime(new Date());
        exam.setJoinType(ExamContant.ZUJUAN_TYPE_REPO);
        exam.setState(ExamContant.OPEN);
        this.baseMapper.insert(exam);

        examSaveDto.getExamRepos().forEach(examRepo -> {
            examRepo.setId(IdWorker.getIdStr());
            examRepo.setId(exam.getId());
            examRepoMapper.insert(examRepo);

        });
        if (examSaveDto.getOpenType() == ExamContant.OPENT_DEPART){
            examSaveDto.getDepartIds().forEach(item->{
                ExamDepart examDepart = new ExamDepart();
                examDepart.setExamId(exam.getId());
                examDepart.setDepartId(item);
                examDepart.setId(IdWorker.getIdStr());
                examDepartMapper.insert(examDepart);
            });
            if (examSaveDto.getOpenType() == ExamContant.OPENT_SPECIFY){
                examSaveDto.getUserIds().forEach(item->{
                    ExamUser examUser = new ExamUser();
                    examUser.setId(IdWorker.getIdStr());
                    examUser.setUserId(item);
                    examUser.setExamId(exam.getId());
                    examUserMapper.insert(examUser);
                });
            }
        }
        return ApiRest.success();
    }

    @Override
    public ApiRest<List<Exam>> myExams() {

        //过去id
        String userId = "1";
        // select * from el_exam where el_exam.open_type =1
        //select * from el_exam let join el_exam_depart on el_exam.id = el_exam_
        //where el_exam_depart.depa
        List<Exam> myExamList = this.baseMapper.findMyExam(userId);


        return ApiRest.success(myExamList);
    }

    private void checkData(ExamSaveDto examSaveDto){
        if (examSaveDto.getTitle() == null){
            throw new ServiceException(ApiErrorEnum.EXAM_CONTENT_IS_NULL.getMsg());
        }
        if (examSaveDto.getQualifyScore() == null){
            throw new ServiceException(ApiErrorEnum.EXAM_QUALIFY_SCORE.getMsg());
        }
        if (examSaveDto.getTotalTime() == null){
            throw new ServiceException(ApiErrorEnum.EXAM_TOTAL_TIME.getMsg());
        }
        if (examSaveDto.getExamRepos() == null){
            throw new ServiceException(ApiErrorEnum.EXAM_REPO_IS_NULL.getMsg());
        }

    }
}
